package cn.zyzpp.controller.resume;

import cn.zyzpp.entity.job.NewJob;
import cn.zyzpp.entity.job.UserJob;
import cn.zyzpp.service.job.JobService;
import cn.zyzpp.service.job.UserJobService;
import cn.zyzpp.service.user.UserService;
import cn.zyzpp.util.StringUtil;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Create by yster@foxmail.com 2018/5/27/027 12:03
 */
@Controller
@RequestMapping("/edit")
public class EditController {
    @Autowired
    private HttpSession session;
    @Autowired
    private UserJobService userJobService;
    @Autowired
    private JobService jobService;
    @Autowired
    private UserService userService;

    /**
     * 评价管理
     */
    @RequestMapping("/editTop/{jid}/{uid}")
    @RequiresRoles("user")
    public String editTop(@PathVariable int jid,@PathVariable int uid ,ModelMap model){
        UserJob userJob = userJobService.queryByUidAndPid(uid, jid);
        model.addAttribute("userJob",userJob);
        return "/edit/editTop";
    }

    /**
     * 评价管理-更新
     */
    @RequestMapping("/editTopEdit/{jid}/{uid}")
    @RequiresRoles("user")
    @ResponseBody
    public String editTopEdit(String text,@PathVariable int jid, @PathVariable int uid){
        int id = StringUtil.getIntBySessionUid(session);
        if (id == uid){
            userJobService.updataUkByUidAndJid(uid,jid,text);
            return "修改成功";
        }
        NewJob newJob = jobService.query(jid);
        if (newJob.getUid() == id){
            userJobService.updataJkByUidAndJid(uid,jid,text);
            return "修改成功";
        }
        return "修改失败";
    }

    /**
     * 应聘管理
     */
    @RequestMapping("/peoTop/{jid}")
    @RequiresRoles("user")
    public String peoTop(@PathVariable int jid, ModelMap model){
        List<UserJob> userJobList =  userJobService.queryByJid(jid);
        for (UserJob user: userJobList) {
            user.setUser(userService.queryAll(user.getUid()));
        }
        model.addAttribute("userJobList",userJobList);
        return "peoTop";
    }
}
